package com.ibl.rover.lite.client.Activity;

import java.util.ArrayList;
import java.util.List;

import com.ibl.rover.lite.client.Logger;
import com.ibl.rover.lite.client.R;
import com.ibl.rover.lite.client.Application.ApplicationContext;
import com.ibl.rover.lite.client.Application.OrderBookingLine;
import com.ibl.rover.lite.client.constants.RoverConstants;
import com.ibl.rover.lite.common.bean.CustomerBean;
import com.ibl.rover.lite.common.bean.OrderBean;
import com.ibl.rover.lite.common.bean.ProductBean;
import com.ibl.rover.lite.common.bean.ProductOrderBean;
import com.ibl.rover.lite.common.db.Criteria;
import com.ibl.rover.lite.common.db.Criteria.Operators;
import com.ibl.rover.lite.common.db.DatabaseManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class CustomerDetailActivity extends Activity implements OnClickListener {

	private ApplicationContext roverApp;
	private Logger logger = new Logger();
	private DatabaseManager manager = null;
	private CustomerBean customerBean;

	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.customer_details);
			roverApp = (ApplicationContext) getApplication();
			manager = roverApp.getDatabaseManager();
			customerBean = (CustomerBean) roverApp.getViewContext().get(
					RoverConstants.CURRENT_CUSTOMER);

			((TextView) findViewById(R.id.id_customer_detail_txt_customer_name))
					.setText(customerBean.getName());

			((TextView) findViewById(R.id.id_customer_detail_txt_customer_code))
					.setText(customerBean.getCode());

			((TextView) findViewById(R.id.id_customer_detail_txt_customer_address))
					.setText(customerBean.getAddress());

			((TextView) findViewById(R.id.id_customer_detail_txt_limit))
					.setText(String.valueOf(customerBean.getCreditLimit()));

			((Button) findViewById(R.id.btn_sale_boooking))
					.setOnClickListener(this);
			((Button) findViewById(R.id.btn_cancel)).setOnClickListener(this);

		} catch (Exception e) {
			logger.logException("On Create exception ", e);
		}

	}

	@Override
	public void onClick(View view) {
		try {
			switch (view.getId()) {
			case R.id.btn_sale_boooking:
				saleBooking();
				break;
			case R.id.btn_cancel:
				finish();
				break;
			default:
				break;
			}
		} catch (Exception e) {
			logger.logException("onClick Exception", e);
		}
	}

	private void saleBooking() {
		try {

			roverApp.getViewContext().put(RoverConstants.CURRENT_CUSTOMER,
					customerBean);
//			isCustomerOrder();
			Intent intent = new Intent(this, SalesBookingActivity.class);
			startActivity(intent);
		} catch (Exception e) {
			logger.logException("salesBooking Excepotion ", e);
		}
	}

	private void isCustomerOrder() {
		try {
			Criteria<OrderBean> criteria = manager
					.generateCriteria(OrderBean.class);
			criteria.addRestriction(OrderBean.COL_CUSTOMER_CODE,
					Operators.isEqual, customerBean.getCode());
			List<OrderBean> orderList = criteria.execute();
			if (orderList.size() > 0) {
				OrderBean orderBean = orderList.get(0);
				Criteria<ProductOrderBean> productOrderBeanCriteria = manager
						.generateCriteria(ProductOrderBean.class);

				productOrderBeanCriteria.addRestriction(
						ProductOrderBean.COL_ORDER_KEY, Operators.isEqual,
						orderBean.getOrderId());

				List<ProductOrderBean> productOrderBeanList = productOrderBeanCriteria
						.execute();
				List<ProductBean> productBeansList = new ArrayList<ProductBean>();
				List<String> productBeansQuantityList = new ArrayList<String>();

				if (productOrderBeanList.size() > 0) {
					Criteria<ProductBean> searchProduct = manager
							.generateCriteria(ProductBean.class);
					searchProduct.addRestriction(ProductBean.COL_PRODUCT_CODE,
							Operators.isEqual, productOrderBeanList.get(0)
									.getProductCode());
					productBeansQuantityList.add(productOrderBeanList.get(0)
							.getQuantity());
					for (int i = 1; i < productOrderBeanList.size(); i++) {
						searchProduct.addRestrictionWithOr(
								ProductBean.COL_PRODUCT_CODE,
								Operators.isEqual, productOrderBeanList.get(i)
										.getProductCode());
						productBeansQuantityList.add(productOrderBeanList
								.get(i).getQuantity());
					}
					productBeansList = searchProduct.execute();

					OrderBookingLine orderBookingLine = new OrderBookingLine();
					orderBookingLine.setProductBeanList(productBeansList);
					orderBookingLine.setOrderAmount(orderBean.getAmount());
					orderBookingLine
							.setProductQuantitylist(productBeansQuantityList);
					roverApp.getViewContext().put(
							RoverConstants.CURRENT_PREVIOUS_ORDER_SUCESS,
							orderBookingLine);
				}

			}

		} catch (Exception e) {
			logger.logException("iS Customer Order Exception", e);
		}
	}
}
